#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

# Login once for all colleges. Ask for report for each college. May have to accumulate up to college from campus level
# warning, we thought we needed to roll up Miami, but Newsbank was already doing it for us. So the rollup code is
# still here, but the login informatoin is now only at the college level.  sigh.
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;

use avsCommon;

dbsetup();

# capture all the college id's in a hash for use during data extraction
my %college;
do {
	$college {$option1} = $collegeid;
} while $sthlogin->fetch;

# used by web user agent
my ($content, $response, $request, $url);
my ($viewstate, $eventvalidation);
my $ua =  new LWP::UserAgent(keep_alive=>1);
$ua->cookie_jar( {} );
$ua->timeout(600);	# 10 minutes... sigh

# request data for year month in optional range. typically beg and end month the same.
# 12 months before requested date is returned in data
my $runyear = $begyear;
my ($begperiodndx, $endperiodndx, $collegeidsave, $collegecode);
my @periods;
my $data;
my %stats;
my $stats;

# run once for each year covered by beg and end date. typically beg and end month the same.
while ($runyear <= $endyear) {

	$collegeidsave = 0;
	%stats = ();
	$stats = ();

	# loop through all the colleges for each data collection. put them back in collegeid order for summation routine
	foreach my $key (sort { $college{$a} <=> $college{$b} } ( keys(%college) )) {
		$collegecode = $key;
		$collegeid = $college{$collegecode};
		$collegeidsave = $collegeid if ! $collegeidsave;

		getStats ();
	}

	while ( (my $datasource, my $value) = each %stats )
	{
		foreach my $i($begperiodndx..$endperiodndx) {
#			my $session = $value->{sessions}[$i];
			my $search  = $value->{searches}[$i];
			print "$vendorid $collegeid $periods[$i] $datasource 0 $search\n";
#			$sthstats->execute($vendorid, $collegeid, $datasource, $periods[$i], $session, 1)
#				or die '#error: sthstats execute 1 failed';
			$sthstats->execute($vendorid, $collegeid, $datasource, $periods[$i], $search, 2 )
				or die '#error: sthstats execute 2 failed';
		}
	}

	# advance to the next year. when we hit the end year, set the end month to end year.
	$runyear++;
}

dbshutdown();

exit ();

sub getStats {
	# get the tab delimited data
	$url = 'http://stats.newsbank.com/stats/servlet/stats.tsv?action=counterDb1&b_id=581600EF43AC4A1BBD7B109F3F9FA0ED&prntOut=1&c_id='.$collegecode.'&output=TSV&year='.$runyear;

	# attempting to overcome timeout issues, by retrying 3 times (16, 32 and 64 seconds sleep)
	my $sleeptime = 16;

	do {
		$response = $ua->request(GET $url);
		if (substr($response->status_line,0,3) eq '500') {
			print "#info: timeout retry $sleeptime\n";
			sleep $sleeptime;
		};

	} until ($response->is_success || ($sleeptime*=2 > 64));

	if (!$response->is_success) {
		die "#error:$0: GET test stats $url :" . $response->status_line;
	}

	$content=$response->decoded_content;

	# save contents for possible debugging
	savecontent('Newsbank', "$collegeid-$collegecode-$runyear", $content);

	my @contents = split ("\n", $content);

	my $datasource;

	# grab the dates off of the header lines, first four columns are not dates
	@periods = ('','','','');	
	my $begperiod = myperiod($runyear, $begmonth);
	my $endperiod = myperiod($endyear, $endmonth);
	$begperiodndx = 4;
	$endperiodndx = 4;

	while ($contents[4] =~ /\t(?<month>\w{3})-(?<year>\d{4})/g) {
		my $period = myperiod($+{year},$+{month});
		$begperiodndx++ if ($period lt $begperiod);
		$endperiodndx++ if ($period lt $endperiod);
		push @periods, $period;
	}
	$begperiodndx-- if $begperiodndx == 16;	# prevent multiyear problem
	$endperiodndx-- if $endperiodndx == 16;	# prevent multiyear problem

#
# some broken down by campus. getstats finds data for each
# college or campus and if needed accumulates the values up to the college level
#

	my @zfill=(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	
	if ($collegeidsave != $collegeid) {
		while ( (my $datasource, my $value) = each %stats )
		{
			foreach my $i ($begperiodndx..$endperiodndx) {
#				my $session = $value->{sessions}[$i];
				my $search  = $value->{searches}[$i];
				print "$vendorid $collegeidsave $periods[$i] $datasource 0 $search\n";
#				$sthstats->execute($vendorid, $collegeidsave, $datasource, $periods[$i], $session, 1)
#					or die '#error: sthstats execute 1.2 failed';
				$sthstats->execute($vendorid, $collegeidsave, $datasource, $periods[$i], $search, 2 )
					or die '#error: sthstats execute 2.2 failed';
			}
		}
		%stats = ();
		$stats = ();
		$collegeidsave = $collegeid;
	}

	# newbank  assigns each college or sometimes campus a number. the regex finds the data after that number
	$datasource='';
	my @columns = ();

	# parse each csv line
	foreach my $content (@contents[5..$#contents]) {

		@columns = split (/\t/, $content);

		next if $#columns<1;

		$datasource = $columns[0];
		$datasource =~ s/\*//g; # remove *

		if ($columns[3] eq 'Searches run') {
			if (! exists $stats{$datasource} ) {
				@{ $stats{$datasource}{searches} } = @zfill;
			}

			foreach my $i ($begperiodndx..$endperiodndx) {
				$stats{$datasource}{searches}[$i] += $columns[$i];
			}
		}
	}
}